unit Unit21; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, sqldb, Unit1, Unit20, LR_Class, DbCtrls, db;

type

  { TForm21 }

  TForm21 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ComboBox1: TComboBox;
    Datasource1: TDatasource;
    DBComboBox1: TDBComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    frReport1: TfrReport;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Memo1: TMemo;
    Memo2: TMemo;
    SQLQuery1: TSQLQuery;
    SQLQuery2: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow;
    procedure frReport1GetValue(const ParName: String; var ParValue: Variant);
  private
    { private declarations }
  public
    codigo : String;
    responsable : string;
    fecha_inicio : String;
    fecha_cierre : String;
  end; 

var
  Form21: TForm21;

implementation

{ TForm21 }

procedure TForm21.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  if SQLQuery2.Active = true then
    SQLQuery2.Close;

  SQLQuery2.SQL.Text := 'select fnc_nombre from funcionario where fnc_descripcion = ''TECNICO'' '
    + 'or fnc_descripcion = ''DEFAULT'' order by fnc_nombre';

  SQLQuery2.Open;

  DBComboBox1.Clear;
  DBComboBox1.DataField := 'fnc_nombre';

  while not SQLQuery2.EOF do
  begin
    DBComboBox1.Items.Add(SQLQuery2.Fields[0].AsString);
    SQLQuery2.Next;
  end;
  
  Form21.Caption := '';
  Edit1.Text := '';
  Edit2.Text := '';
  DBComboBox1.Text := responsable;
  Memo1.Text := '';
  Memo2.Text := '';
  ComboBox1.Text := '';

  if codigo <> '' then
  begin
    SQLQuery1.SQL.Text := 'select cln_nombre from vw_incidente where inc_cdg = ' + codigo;
    
    SQLQuery1.Open;
    
    while not SQLQuery1.EOF do
    begin
      Edit1.Text := SQLQuery1.Fields[0].AsString;
      SQLQuery1.Next;
    end;
    
    SQLQuery1.Close;
    
    SQLQuery1.SQL.Text := 'select * from incidente where inc_cdg = ' + codigo;
    
    SQLQuery1.Open;
    
    while not SQLQuery1.EOF do
    begin
      Form21.Caption := 'Incidente: ' + SQLQuery1.Fields[0].AsString + ' - Fecha de Inicio: '
        + SQLQuery1.Fields[1].AsString;
      fecha_inicio := SQLQuery1.Fields[1].AsString;
      Edit2.Text := SQLQuery1.Fields[3].AsString;
      Memo1.Text := SQLQuery1.Fields[4].AsString;
      Memo2.Text := SQLQuery1.Fields[5].AsString;
      ComboBox1.Text := SQLQuery1.Fields[6].AsString;

      if SQLQuery1.Fields[7].AsString <> '' then
      begin
        fecha_cierre :=  SQLQuery1.Fields[7].AsString;
        Form21.Caption := Form21.Caption + ' - Fecha de Cierre: ' + SQLQuery1.Fields[7].AsString;
      end;
      
      SQLQuery1.Next;
    end;
    
    Memo2.SetFocus;
  end
  else
    Close;
end;

procedure TForm21.frReport1GetValue(const ParName: String; var ParValue: Variant
  );
begin
  if ParName = 'codigo' then
    ParValue := codigo;

  if ParName = 'fecha_inicio' then
    ParValue := fecha_inicio;

  if ParName = 'fecha_cierre' then
    ParValue := fecha_cierre;
    
  if ParName = 'cliente' then
    ParValue := Edit1.Text;

  if ParName = 'titulo' then
    ParValue := Edit2.Text;

  if ParName = 'tecnico' then
    ParValue := DBComboBox1.Text;

  if ParName = 'estado' then
    ParValue := ComboBox1.Text;

  if ParName = 'detalle' then
    ParValue := Memo1.Text;

  if ParName = 'desarrollo' then
    ParValue := Memo2.Text;
end;

procedure TForm21.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm21.FormCreate(Sender: TObject);
begin
    SQLQuery1.DataBase := Form1.PQConnection1;
    SQLQuery1.Transaction := Form1.SQLTransaction1;
    
    SQLQuery2.DataBase := Form1.PQConnection1;
    SQLQuery2.Transaction := Form1.SQLTransaction1;
end;

procedure TForm21.Button3Click(Sender: TObject);
begin
  Close;
end;

procedure TForm21.Button1Click(Sender: TObject);
begin
  frReport1.LoadFromFile('reporte_incidencia.lrf');
  frReport1.ShowReport;
end;

procedure TForm21.Button2Click(Sender: TObject);
var
  QueryString : String;

begin
  if(Edit1.Text <> '') and (ComboBox1.Text <> '') and (Edit2.Text <> '') and (Memo1.Text <> '') then
  begin
    QueryString := 'update incidente set inc_desarrollo = ''' + Memo2.Text
      + ''', inc_estado =  ''' + ComboBox1.Text + ''', inc_responsable = '
      + '(select fnc_cdg from funcionario where fnc_nombre = ''' + DBComboBox1.Text
      + ''') where inc_cdg = ' + codigo;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('MODIFICACIÓN EXITOSA');
    Close;
  end
  else
    ShowMessage('ERROR EN EL INGRESO DE DATOS');
end;

initialization
  {$I unit21.lrs}

end.

